package quickConnect;

import java.awt.FileDialog;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JFileChooser;

import org.quickconnect.ControlObject;

import userInterface.ChatSessionPanel;
import userInterface.MainFrame;

import beans.FileBean;

public class DownloadFileBCO implements ControlObject {

	@Override
	public Object handleIt(ArrayList<Object> arg0) {

		//HashMap params = (HashMap) arg0.get(0);
		//FileBean fileBean = (FileBean) params.get("file");
		File file = (File) arg0.get(arg0.size()-1);
		
		FileDialog dialog = new FileDialog(MainFrame.mainFrame, "Save File", FileDialog.SAVE);
		ChatSessionPanel panel = (ChatSessionPanel) MainFrame.mainFrame.getTabs().getSelectedComponent();
		String fileName = (String) panel.getaFilePanel().getTable().getModel().getValueAt(panel.getaFilePanel().getTable().getSelectedRow(), 0);
		dialog.setFile(fileName);
		dialog.setVisible(true);
		String delims = "[.]";
		String[] tokens = fileName.split(delims);
		if (dialog.getFile() != null) {
			File theFileToSave = new File(dialog.getDirectory() + 
			System.getProperty("file.separator") + dialog.getFile() + "." + tokens[tokens.length - 1]);
			try {
				copy(file, theFileToSave);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
//		JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
//		ChatSessionPanel panel = (ChatSessionPanel) MainFrame.mainFrame.getTabs().getSelectedComponent();
//		String fileName = (String) panel.getaFilePanel().getTable().getModel().getValueAt(panel.getaFilePanel().getTable().getSelectedRow(), 0);
//		fileChooser.setSelectedFile(new File(fileName));
//		int returnVal = fileChooser.showSaveDialog(null);  
//		
//		if(returnVal == JFileChooser.APPROVE_OPTION){
//			if(fileChooser.getSelectedFile()!=null){
//				File theFileToSave = fileChooser.getSelectedFile();
//				try {
//					copy(file,theFileToSave);
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		}
		
		return null;
	}
	
	void copy(File src, File dst) throws IOException {
	    InputStream in = new FileInputStream(src);
	    OutputStream out = new FileOutputStream(dst);

	    // Transfer bytes from in to out
	    byte[] buf = new byte[1024];
	    int len;
	    while ((len = in.read(buf)) > 0) {
	        out.write(buf, 0, len);
	    }
	    in.close();
	    out.close();
	}

}
